package cn.tedu.crm.orders.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.orders.pojo.dto.*;
import cn.tedu.crm.orders.pojo.vo.OrderDetailVO;
import cn.tedu.crm.orders.pojo.vo.OrdersVO;
import cn.tedu.crm.orders.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/api/crm/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @SaCheckPermission("crm:order:select")
    @GetMapping("/load")
    public JsonResult<PageObject<OrdersVO>> load(OrdersQuery ordersQuery){
        log.debug("加载订单信息:{}",ordersQuery);
        PageObject<OrdersVO> pageObject = PageObject.page(ordersQuery.getPageNum(), ordersQuery.getPageSize(), () -> {
            return ordersService.load(ordersQuery);
        });
        return JsonResult.ok(pageObject);
    }
//    @SaCheckPermission("crm:order:add")
    @PostMapping("/add")
    public JsonResult add(@RequestBody OrdersAddParam ordersAddParam){
        log.debug("添加订单信息:{}",ordersAddParam);
        ordersService.add(ordersAddParam);
        return JsonResult.ok();
    }
    @SaCheckPermission("crm:order:del")
    @GetMapping("/delete")
    public JsonResult delete(Long id){
        log.debug("订单删除:{}",id);
        ordersService.delete(id);
        return JsonResult.ok();
    }

//    @SaCheckPermission("crm:order:add")
    @PostMapping("/addScheme")
    public JsonResult addScheme(@RequestBody OrdersSchemeParam ordersSchemeParam){
        log.debug("订单方案预演记录提交:{}",ordersSchemeParam);
        ordersService.addScheme(ordersSchemeParam);
        return JsonResult.ok();
    }

//    @SaCheckPermission("crm:order:add")
    @PostMapping("/refundAdd")
    public JsonResult refundAdd(@RequestBody OrdersRefundParam ordersRefundParam){
        log.debug("订单退款提交:{}",ordersRefundParam);
        ordersService.refundInsert(ordersRefundParam);
        return JsonResult.ok();
    }
    @SaCheckPermission("crm:order:select")
    @GetMapping("/details")
    public JsonResult<OrderDetailVO> details(Long id){
        log.debug("订单详情:{}",id);
        OrderDetailVO detailVO = ordersService.details(id);
        return JsonResult.ok(detailVO);
    }
    @GetMapping("/getUserId")
    public JsonResult<Long> getUserId(Long id){
        Long userId = ordersService.getUserId(id);
        return JsonResult.ok(userId);
    }


    @PostMapping("/dieOrder")
    public JsonResult dieOrder(DieOrderParam param){
        log.debug("订单终止:{}", param);
        ordersService.dieOrder(param);
        return JsonResult.ok();
    }

//    @PostMapping("/upload")
//    public JsonResult upload(@PathVariable Long id){
//        log.debug("订单图纸提交:{}",id);
//        ordersService.upload(uploadParam);
//        return JsonResult.ok();
//    }
}
